<Switch
android:id="@+id/switch1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Switch"
android:layout_margin="20dp"
android:textSize="22dp"
app:layout_constraintBottom_toTopOf="@id/textView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開啟"
android:textSize="22dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
switch1.setOnCheckedChangeListener { buttonView, isChecked ->
when(isChecked){
true -> textView.text = "開啟"
false -> textView.text = "關閉"
}
}
}